<?php

/**
 * This is the model class for table "{{news}}".
 *
 * The followings are the available columns in table '{{news}}':
 * @property integer $zne_id
 * @property integer $znc_id
 * @property integer $zm_id
 * @property integer $zca_id
 * @property integer $zar_id
 * @property string $zne_title
 * @property string $zne_short
 * @property string $zne_desc
 * @property string $zne_content
 * @property string $zne_source
 * @property string $zne_thumb
 * @property string $zne_photo
 * @property string $zne_video_url
 * @property integer $zne_focus
 * @property integer $zne_commend
 * @property integer $zne_highlight
 * @property integer $zne_hits
 * @property integer $zne_status
 * @property string $zt_ids
 * @property integer $zne_page
 * @property string $zne_jumplink
 * @property integer $inputtime
 * @property integer $updatetime
 */
class News extends CActiveRecord
{
	//关键字
	public $zne_tag;
	
	//查询方式 发布者
	public $author_type;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return News the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{news}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('znc_id,zne_title,zne_short,zne_content', 'required'),
			array('znc_id, zm_id, author_type, zca_id, zar_id, zne_focus, zne_commend, zne_highlight, zne_hits, zne_status, zne_page, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zne_title, zne_desc, zne_source, zne_tag', 'length', 'max'=>255),
			array('zne_short', 'length', 'max'=>100),
			array('zne_thumb, zne_jumplink', 'length', 'max'=>100),
			array('zne_video_url', 'length', 'max'=>255),
			array('zne_content, zt_ids', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zne_id, znc_id, zm_id, zca_id, zar_id, zne_title, zne_short, zne_desc, zne_content, zne_source, zne_thumb, zne_photo, zne_video_url, zne_focus, zne_commend, zne_highlight, zne_hits, zne_status, zt_ids, zne_page, zne_jumplink, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'newscat'=>array(self::BELONGS_TO, 'NewsCat', 'znc_id'),
				'member'=>array(self::BELONGS_TO, 'Member', 'zm_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zne_id' => 'Zne',
			'znc_id' => '信息分类',
			'zm_id' => 'Zm',
			'zca_id' => 'Zca',
			'zar_id' => 'Zar',
			'zne_title' => '信息标题',
			'zne_short' => '短标题',
			'zne_desc' => 'Zne Desc',
			'zne_content' => 'Zne Content',
			'zne_source' => 'Zne Source',
			'zne_thumb' => 'Zne Thumb',
			'zne_video_url' => 'Zne Video Url',
			'zne_focus' => 'Zne Focus',
			'zne_commend' => 'Zne Commend',
			'zne_highlight' => 'Zne Highlight',
			'zne_hits' => 'Zne Hits',
			'zne_status' => 'Zne Status',
			'zt_ids' => 'Zt Ids',
			'zne_page' => 'Zne Page',
			'zne_jumplink' => 'Zne Jumplink',
			'inputtime' => 'Inputtime',
			'updatetime' => 'Updatetime',
			'zne_tag' => '关键字',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		if($this->zne_title=='输入信息标题关健字'){
			$this->zne_title = '';
		}
		
		$criteria->compare('zne_id',$this->zne_id);
		
		if(!empty($this->author_type)){
			if($this->author_type==2){
				$criteria->compare('zm_id','0');
			}elseif($this->author_type==3){
				$criteria->compare('zm_id','>0');
			}
		}else{
			$criteria->compare('zm_id',$this->zm_id);
		}
		$criteria->compare('zca_id',$this->zca_id);
		$criteria->compare('zar_id',$this->zar_id);
		$criteria->compare('zne_title',$this->zne_title,true);
		$criteria->compare('zne_short',$this->zne_short,true);
		$criteria->compare('zne_desc',$this->zne_desc,true);
		$criteria->compare('zne_content',$this->zne_content,true);
		$criteria->compare('zne_source',$this->zne_source,true);
		$criteria->compare('zne_thumb',$this->zne_thumb,true);
		$criteria->compare('zne_video_url',$this->zne_video_url,true);
		$criteria->compare('zne_focus',$this->zne_focus);
		$criteria->compare('zne_commend',$this->zne_commend);
		$criteria->compare('zne_highlight',$this->zne_highlight);
		$criteria->compare('zne_hits',$this->zne_hits);
		$criteria->compare('zne_status',$this->zne_status);
		$criteria->compare('zt_ids',$this->zt_ids,true);
		$criteria->compare('zne_page',$this->zne_page);
		$criteria->compare('zne_jumplink',$this->zne_jumplink,true);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);
		$criteria->compare('zne_tag',$this->zne_tag);
		
		if(!empty($this->znc_id)){
			$criteria->join = ' LEFT JOIN {{news_cat}} nc ON t.znc_id=nc.znc_id';
			$criteria->addCondition(" (nc.znc_id={$this->znc_id} OR nc.znc_patentid={$this->znc_id} OR nc.znc_topid={$this->znc_id}) ");
		}else{
			$criteria->compare('znc_id',$this->znc_id);
		}

		$sort = new CSort();
  		$sort->attributes = array(
    		'articleCount'=>array(
      			'asc'=>'articleCountASC',
      			'desc'=>'articleCountDESC',
    		),
    		'*', // add all of the other columns as sortable
  		);
  		$criteria->order = "inputtime DESC";//默认排序为时间倒序
	  	return new CActiveDataProvider(get_class($this), array(
	    	'criteria'=>$criteria,
	    	'sort'=>$sort,
	    	'pagination'=> array(
	      		'pageSize'=>20,
	    	),
  		));
	}
	
	/**
	 * 获取分布方式
	 */
	public function getZnePage($type=null){
		if($type==null){
			return array(
					'1'=>'否',
					'2'=>'是',
				);
		}else{
			$level = $this->getZnePage();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 是否头条
	 */
	public function getZneFocus($type=null){
		if($type==null){
			return array(
					'1'=>'否',
					'2'=>'是',
			);
		}else{
			$level = $this->getZneFocus();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 是否推荐
	 */
	public function getZneCommend($type=null){
		if($type==null){
			return array(
					'1'=>'否',
					'2'=>'是',
			);
		}else{
			$level = $this->getZneCommend();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 保存前的操作，
	 */
	protected function beforeSave(){
		if(!empty($this->zne_tag)){
			$this->zne_tag = str_replace('，',',',$this->zne_tag);
			$tag = explode(',',$this->zne_tag);
			foreach($tag as $val){
				$tagModel = Tag::model()->findPyAttributes(array('zt_title'=>$val));
				if(empty($tagModel)){
					$tagModel = new Tag();
					$tagModel->zt_title = $val;
					$tagModel->save();
				}
				$tagId[] =$tagModel->zt_id;
			}
			$this->zne_tag->join(',',$tagId);
		}
		if($this->isNewRecord){
			$this->inputtime  = time();
			$this->zne_status = 2;
		}else{
			$this->updatetime = time();
		}
		return true;
	}
	
	/**
	 * 发布者
	 */
	public function getAuthorType($type=null){
		if($type==null){
			return array(
				'1'=>'全部',
				'2'=>'商城',
				'3'=>'商家',
			);
		}else{
			$level = $this->getAuthorType();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 根据资讯条件获取相应内容
	 * $condition 条件
	 * $limit 返回几条数据
	 * @return Array
	 */
	public function getNewsData($condition, $limit='', $offset='', $order='zne_id DESC')
	{
		return News::model()->findAll(array('condition'=>$condition,'order'=>$order, 'limit'=>$limit, 'offset'=>$offset));
	}
	
}